package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import selenium.WebDriverWrapper;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by sergey on 05.07.2014.
 */
public class ComparisonPage extends BasePage {
    private static final By PRESENCE_OF_ITEM_LOCATOR = By.xpath("//div[@class = 'item available']");
    private static final By DETAILED_COMPARISON_LINK_LOCATOR = By.xpath("//ul[@class='menu-comparison']/li/h3/a");
    private static final By COMPARE_ONLY_DIFFERENCE_LOCATOR = By.xpath("//ul[@class='menu-sort-filter']/li[2]/a");
    private static final By KEY_LOCATOR = By.xpath("//td[@class='detail-title']");
    private static final String VALUE_LOCATOR = "//td[@class='detail-title']/ancestor::tr/td[%d]";
    private static final int PRODUCT1_NUMBER = 2;
    private static final int PRODUCT2_NUMBER = 3;
    public ComparisonPage(WebDriverWrapper driver)
    {
        super (driver);
        URL = "http://rozetka.com.ua/comparison/";
        driver.get(URL);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    }

    public void detailComparison()
    {
        driver.findElement(DETAILED_COMPARISON_LINK_LOCATOR).click();
    }
    public boolean itemsPresence()
    {
        return driver.findElements(PRESENCE_OF_ITEM_LOCATOR).size()>1;
    }
    public void compareOnlyDifferenceInProducts()
    {
        driver.findElement(COMPARE_ONLY_DIFFERENCE_LOCATOR).click();
    }

    public List<WebElement>  getListOfProductNameFields()
    {
        return driver.findElements(KEY_LOCATOR);

    }
    public List<WebElement>  getListOfProduct1Fields()
    {
        return driver.findElements(By.xpath(String.format(VALUE_LOCATOR,PRODUCT1_NUMBER)));

    }
    public List<WebElement>  getListOfProduct2Fields()
    {
       return driver.findElements(By.xpath(String.format(VALUE_LOCATOR,PRODUCT2_NUMBER)));

    }
    public String[] getOnlyDifferentNameFields()
    {

        List<WebElement> names = getListOfProductNameFields();
        List<WebElement> firstProductFields = getListOfProduct1Fields();
        List<WebElement> secondProductFields = getListOfProduct2Fields();
        String[] differentNames = new String[names.size()];
        int j = 0;
        for (int i = 0; i < names.size(); i++)
        {
            if(!firstProductFields.get(i).equals(secondProductFields.get(i)))
            {
                differentNames[j] = names.get(i).getText();
            }
        }
        return differentNames;
    }
    public String[] checkForDifferenceBetweenNameOfFields()
    {
        List<WebElement> newNames = getListOfProductNameFields();
        String[] newNamesForCheck = new String[newNames.size()];
        for (int k = 0; k < newNames.size(); k++ )
        {
          newNamesForCheck[k]=newNames.get(k).getText();
        }
        return newNamesForCheck;
    }



}
